package test.lemonyfei.http.utils;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by sh00556 on 2017/4/25.
 */
public class HttpClientUtil {
    private CloseableHttpClient httpclient;

    public static String doGet(String url, HashMap<String, String> headers) {
        HttpClientUtil httpClient = new HttpClientUtil();
        httpClient.init();
        String response = httpClient.get(url, headers);
        httpClient.close();
        return response;
    }

    public static String doPost(String url, Map<String, String> headers, String params) {
        HttpClientUtil httpClient = new HttpClientUtil();
        httpClient.init();
        String response = httpClient.post(url, headers, params);
        httpClient.close();
        return response;
    }

    private void init() {
        this.httpclient = HttpClientBuilder.create().build();
    }

    private String get(String url, HashMap<String, String> headers) {
        String reponseString = new String();
        try {
            HttpGet httpget = new HttpGet(url);
            if ((headers != null) && (!headers.isEmpty())) {
                Iterator<Map.Entry<String, String>> iter = headers.entrySet().iterator();
                while (iter.hasNext()) {
                    Map.Entry<String, String> entry = (Map.Entry) iter.next();
                    httpget.setHeader((String) entry.getKey(), (String) entry.getValue());
                }
            }
            CloseableHttpResponse response = this.httpclient.execute(httpget);
            try {
                HttpEntity entity = response.getEntity();
                System.out.println(response.getStatusLine());
                if (entity != null) {
                    reponseString = EntityUtils.toString(entity, "utf-8");
                }
            } finally {
                response.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return reponseString;
    }

    private String post(String url, Map<String, String> headers, String params) {
        String reponseString = "";
        try {
            HttpPost httppost = new HttpPost(url);
            if ((headers != null) && (!headers.isEmpty())) {
                Iterator<Map.Entry<String, String>> iter = headers.entrySet().iterator();
                while (iter.hasNext()) {
                    Map.Entry<String, String> entry = (Map.Entry) iter.next();
                    httppost.setHeader(entry.getKey(), entry.getValue());
                }
            }
            httppost.setEntity(new StringEntity(params));
            CloseableHttpResponse response = this.httpclient.execute(httppost);
            try {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    reponseString = EntityUtils.toString(entity, "gbk");
                }
            } finally {
                response.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return reponseString;
    }

    private void close() {
        try {
            this.httpclient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
